package com.itheima.natives;

import java.sql.*;

/**
 * @author wz
 * @description:
 * @date 2021-10-31
 */
public class JdbcDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 获取连接对象
        String url = "jdbc:mysql:///heima01";
        String username = "root";
        String password = "1234";
        Connection conn = DriverManager.getConnection(url, username, password);
        // 获取预编译对象
        String sql = "select * from account where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        // 注入参数 进行查询
        ps.setInt(1, 2);
        ResultSet query = ps.executeQuery();
        while ((query.next())) {
            int id = query.getInt("id");
            String name = query.getString("name");
            double money = query.getDouble(3);
            System.out.println(id + " " + name + " " + money);
        }
        query.close();
        ps.close();
        conn.close();
    }
}
